package com.easipass.framework.eirframeworkcoredsl;

import java.util.Collections;
import java.util.List;

public class SplitNode extends LabelledServiceNode {

    private List<LabelledServiceNode> labelledServiceNodes;

    SplitNode(int startpos, int endpos, List<LabelledServiceNode> parallelSequences) {
        super(startpos, endpos);
        this.labelledServiceNodes = Collections.unmodifiableList(parallelSequences);
    }

    @Override
    public String stringify(boolean includePositionInfo) {
        StringBuilder s = new StringBuilder();
        s.append(TokenKind.LT.tokenChars);
        for (int i = 0; i < labelledServiceNodes.size(); i++) {
            LabelledServiceNode jn = labelledServiceNodes.get(i);
            if (i > 0) {
                s.append(" ").append(TokenKind.DOUBLEPIPE.tokenChars).append(" ");
            }
            s.append(jn.stringify(includePositionInfo));
        }
        s.append(TokenKind.GT.tokenChars);
        return s.toString();
    }

    @Override
    public int getSeriesLength() {
        return labelledServiceNodes.size();
    }

    @Override
    public LabelledServiceNode getSeriesElement(int index) {
        return labelledServiceNodes.get(index);
    }

    @Override
    public List<LabelledServiceNode> getSeries() {
        return labelledServiceNodes;
    }

    @Override
    public boolean isSplit() {
        return true;
    }

    @Override
    public String toString() {
        return "[Split:" + stringify(true) + "]";
    }

    @Override
    public void accept(Visitor visitor) {
        boolean cont = visitor.preVisit(this);
        if (!cont) {
            return;
        }
        visitor.visit(this);
        for (LabelledServiceNode labelledTaskNode : labelledServiceNodes) {
            labelledTaskNode.accept(visitor);
        }
        visitor.postVisit(this);
    }
}
